package com.utils;


import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;

/**
 * 返回数据
 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        put("code", 0);
        put("message", "success");
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static R noLogin(){
    	return error(401, "当前用户暂未登陆,请重新登陆");
    }
    
    public static R error(String msg) {
        return error(1, msg);
    }


    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("message", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("message", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    /**
     * 返回结果
     *
     * @param returnMap
     * @return
     */
    public static R success(Object returnMap) {
        R r = new R();
        r.put("data", returnMap);
        return r;
    }


    /**
     * 王志亮 添加获取数据方法
     */

    public int getCode() {
        return (int) this.get("code");
    }

    public String getMsg() {
        return (String) this.get("message");
    }

    public Object getData() {
        return this.get("data");
    }

	public static R returnSuccPage(Page pageBean) {

		Map<String, Object> dataList = new HashMap<String, Object>(5);
		dataList.put("list", pageBean.getRecords());
		dataList.put("totalCount", pageBean.getTotal());
		
		return R.success(dataList);
	}

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
